#!/usr/bin/env python
################################################################
#
# list_config:
#   prints a brief list of all configurations in a potfit config file
#
################################################################
#
#   Copyright 2013
#             Institute for Theoretical and Applied Physics
#             University of Stuttgart, D-70550 Stuttgart, Germany
#             https://www.potfit.net/
#
#################################################################
#
#   This file is part of potfit.
#
#   potfit is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   potfit is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with potfit; if not, see <http://www.gnu.org/licenses/>.
#
#################################################################

import argparse
import os
import sys

parser = argparse.ArgumentParser(description='Prints a brief list of all configurations in a potfit config file.')
parser.add_argument('filename', type=str, help='name of the potfit config file')
args = parser.parse_args()

# open configuration file
try:
    f = open(args.filename)
except IOError as e:
    sys.stderr.write("Could not open the configuration file:\n")
    sys.stderr.write("%s\n" % e)
    sys.exit()

data_N = []
data_C = []
data_W = []
origin = []
lines = []
i = 1

for line in f:
    if line[0] == '#':
        temp = line.strip().split()
        if line[1] == 'N':
            data_N.append(int(temp[1]))
            data_C.append([])
            data_W.append(1)
            origin.append([])
            lines.append(i)
        if line[1] == 'C':
            data_C[-1] = temp[1:]
        if line[1] == 'W':
            data_W[-1] = float(temp[1])
        if line[1] == '#' and 'generated' in line:
            origin[-1] = line
    i += 1

for i in range(len(data_N)):
    if origin[i] == '' or not origin[i]:
        source = 'source unknown'
    else:
        source = 'generated from {}'.format(origin[i].strip().split()[6])
    if not data_C[i]:
        elements = 'unknown types'
    else:
        elements = (', ').join(data_C[i])
    print "{:3}: {}".format(i,source)
    print "\tcontaining {} atoms ({}), weight {}, starting on line {}".format(data_N[i],elements,data_W[i],lines[i])
